﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.WS.UserControls;
using GCELibrary.Current;
using System.Transactions;
using ZF.BLL;
namespace ZF.WS.Admin
{
    public partial class NewsManage : ZF.BLL.Page.MMPage
    {
        public override ModuleType Module
        {
            get { return ModuleType.SysNews; }
            protected set
            {
                base.Module = value;
            }
        }


        [PageArgument(Key = "nid", CanNull = false, BindOperType = PageOperType.Edit)]
        public int NewsId { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {
                if (this.OperType == PageOperType.AddNew)
                    Add();
                else if (this.OperType == PageOperType.Edit)
                    Edit();
            }
        }
        public void InitControls()
        {
            this.ddlType.DataSource = DictionaryConfig.INewType;
            this.ddlType.DataBind();
        }

        public void Add()
        {
            InitControls();

        }
        public void AddSubmit(object sender, EventArgs e)
        {


            using (var dc = SiteInfo.CreateDBDataContext())
            {


                var news = new ZF.Model.I_News()
                {
                    AddTime = DateTime.Now,
                    Author = this.tbAuthor.Text,
                    Content = this.FCKeditor1.Value,
                    Focus = this.cbFocus.Checked,
                    ComeFrom = this.tbFrom.Text,
                    Hits = 0,
                    Overhead = this.cbOverhead.Checked,
                    Title = this.tbTitle.Text,
                    Type = this.ddlType.SelectedValue,
                    Photo = this.SWFUpLoad1.UpLoadFileInfos.ConstituteString(";"),
                    IsV=true,
                };

             
                try
                {
                    dc.I_News.InsertOnSubmit(news);
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    throw WarnFactory.CreateWarn(ex);
                }
                string msgKey = "新闻资讯";

                SiteInfo.InsertLog("添加" + msgKey, null);
                WarnMessageLink link = new WarnMessageLink(string.Format("成功添加一个{0}，点击确定按钮返回上一页", msgKey));
                link.AddWarnMessageLink(string.Format("继续添加{0}", msgKey),
                        Request.Url.ToString());
                WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success,
                    string.Format("{0}/news/newslist.aspx?mt=100", AppKey.SiteKey.ManageDirectory)).Alert();

            }

        }

        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {

                var news = dc.I_News.FirstOrDefault(p => p.NewsId == this.NewsId);
                if (news == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);

                InitControls(); ;

                this.tbAuthor.Text = news.Author;
                this.FCKeditor1.Value = news.Content;
                this.cbFocus.Checked = news.Focus;
                this.tbFrom.Text = news.ComeFrom;
                this.cbOverhead.Checked = news.Overhead;
                this.tbTitle.Text = news.Title;
                this.ddlType.SelectedValue = news.Type;
                this.tbPhoto.Text = news.Photo;
                this.lvImgList.DataSource = news.Photo.EmptyString("").Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(p => new
                    {
                        File = p
                    });
                this.lvImgList.DataBind();




            }
        }
        public void EditSubmit(object sender, EventArgs e)
        {

            using (var ts = new System.Transactions.TransactionScope())
            {
                using (var dc = BLL.SiteInfo.CreateDBDataContext())
                {

                    var news = dc.I_News.FirstOrDefault(p => p.NewsId == this.NewsId);
                     if (news == null)
                         throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
           
                  
                        news.Author = this.tbAuthor.Text;
                        news.Content = this.FCKeditor1.Value;
                        news.Focus = this.cbFocus.Checked;
                        news.ComeFrom = this.tbFrom.Text;
              
                        news.Overhead = this.cbOverhead.Checked;
                        news.Title = this.tbTitle.Text;
                        news.Type = this.ddlType.SelectedValue;
                        news.Photo = this.SWFUpLoad1.UpLoadFileInfos.ConstituteString(";");

                        var photoList = this.tbPhoto.Text.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
                        photoList.AddRange(this.SWFUpLoad1.UpLoadFileInfos);
                        news.Photo = photoList.ConstituteString(";"); ;
                    try
                    {
                     
                        dc.SubmitChanges();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {
                        throw WarnFactory.CreateWarn(ex);
                    }
                    string msgKey = "新闻资讯";

  

                    SiteInfo.InsertLog("修改" + msgKey, null);
                    WarnMessageLink link = new WarnMessageLink(string.Format("成功添加一个{0}，点击确定按钮返回{0}列表", msgKey));
                    //link.AddWarnMessageLink(string.Format("查看该{0}", msgKey),
                    //    string.Format("~/news/index.aspx?id={0}", news.NewsId));
                    link.AddWarnMessageLink(string.Format("重新修改{0}", msgKey),
                        Request.Url.ToString());
                    WarnFactory.CreateWarn(link.ToString(), WarnIcon.Success,
                        string.Format("{0}/news/newslist.aspx?mt=100", AppKey.SiteKey.ManageDirectory)).Alert();




                     





                }


            }

        }

    
    }
}
